package org.cdc.divination.frame.data;


import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

public class TextFormat {
    private static TextFormat instance;
    public static TextFormat getDefaultTextFormat(){
        if (instance == null) instance = new TextFormat();
        return instance;
    }
    private TextFormat(){
        dataPool = true;
    }
    private boolean dataPool;
    private Map<String,String> vars;
    public TextFormat(final Map<String,String> vars){
        this.vars = vars;
    }
    public String format( final String text){
        AtomicReference<String> finishText = null;
        if (dataPool){
            DataPool.getPublicDataPool().keySet().stream().forEach(a->{
                finishText.set(text.replaceAll("\\["+a+"\\]", DataPool.getPublicData(a).toString()));
            });
        } else{
            vars.keySet().stream().forEach(a->{
                finishText.set(text.replaceAll("\\["+a+"\\]",vars.get(a)));
            });
        }
        return finishText.get();
    }
}
